namespace SimpleWebServices.Web.Mvc
{
    using System;
    using System.Globalization;
    using System.Threading;
    using System.Web;
    using System.Web.Mvc;
    using System.Web.Routing;
    using Moq;
    using SimpleWebServices;
    using SimpleWebServices.Data;
    using SimpleWebServices.Models;
    using SimpleWebServices.Web;
    using Xunit;

    public sealed class TenantTemporaryRedirectControllerFacts
    {
        [Fact]
        public void typedef()
        {
            Assert.True(new ObjectExpectations<TenantTemporaryRedirectController>()
                .DerivesFrom<TenantController<TenantTemporaryRedirect>>()
                .IsConcreteClass()
                .IsSealed()
                .HasDefaultConstructor()
                .IsDecoratedWith<InternalServerErrorAttribute>()
                .Allow("GET, HEAD, OPTIONS")
                .IsDecoratedWith<CacheControlAttribute>()
                .IsDecoratedWith<ContentLanguageAttribute>()
                .IsDecoratedWith<ContentLocationAttribute>()
                .IsDecoratedWith<ContentMD5Attribute>()
                .Result);
        }

        [Fact]
        public void ctor()
        {
            Assert.NotNull(new TenantTemporaryRedirectController());
        }

        [Fact]
        public void op_HtmlRepresentation_CultureInfo()
        {
            FakeRepository.Clear();

            CultureInfo culture = Thread.CurrentThread.CurrentUICulture;

            try
            {
                FakeRepository.Register<Tenant>(new
                {
                    urn = "urn://example.com/tenant/sample",
                    xml = "<tenant />"
                });
                FakeRepository.Register<TenantTemporaryRedirect>(new
                {
                    urn = "urn://example.com/temporary-redirect/test",
                    xml = "<redirection location='{tenant-path}/path' />"
                });

                var mock = new Mock<HttpContextBase>();
                mock.SetupGet<string>(x => x.Request.Path).Returns("/sample/test.en.html").Verifiable();
                HttpContextFactory.Mock = mock.Object;

                TenantTemporaryRedirectController controller = new TenantTemporaryRedirectController();
                controller.ControllerContext = new ControllerContext(mock.Object, new RouteData(), controller);

                TemporaryRedirectResult result = controller.HtmlRepresentation(new CultureInfo("en")) as TemporaryRedirectResult;

                Assert.Equal<string>("/sample/path", result.Location);

                mock.VerifyAll();
            }
            finally
            {
                Thread.CurrentThread.CurrentUICulture = culture;

                FakeRepository.Reset();
                HttpContextFactory.Reset();
            }
        }

        [Fact]
        public void op_XhtmlRepresentation_CultureInfo()
        {
            FakeRepository.Clear();

            CultureInfo culture = Thread.CurrentThread.CurrentUICulture;

            try
            {
                FakeRepository.Register<Tenant>(new
                {
                    urn = "urn://example.com/tenant/sample",
                    xml = "<tenant />"
                });
                FakeRepository.Register<TenantTemporaryRedirect>(new
                {
                    urn = "urn://example.com/temporary-redirect/test",
                    xml = "<redirection location='{tenant-path}/path' />"
                });

                var mock = new Mock<HttpContextBase>();
                mock.SetupGet<string>(x => x.Request.Path).Returns("/sample/test.en.xhtml").Verifiable();
                HttpContextFactory.Mock = mock.Object;

                TenantTemporaryRedirectController controller = new TenantTemporaryRedirectController();
                controller.ControllerContext = new ControllerContext(mock.Object, new RouteData(), controller);

                TemporaryRedirectResult result = controller.XhtmlRepresentation(new CultureInfo("en")) as TemporaryRedirectResult;

                Assert.Equal<string>("/sample/path", result.Location);

                mock.VerifyAll();
            }
            finally
            {
                Thread.CurrentThread.CurrentUICulture = culture;

                FakeRepository.Reset();
                HttpContextFactory.Reset();
            }
        }
    }
}